cmake_minimum_required(VERSION 3.14)

project(xxx VERSION 1.0.0 LANGUAGES C CXX)

# ============================================================================
# Compiler Settings
# ============================================================================
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_C_COMPILER "clang")
set(CMAKE_CXX_COMPILER "clang++")

# Build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type" FORCE)
endif()
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")

# Compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")

# ============================================================================
# Path Configuration
# ============================================================================
# Define base paths
set(SERVER_ROOT_PATH ${PROJECT_SOURCE_DIR}/..)
set(PLUGINS_PATH ${SERVER_ROOT_PATH}/plugins)
set(RUNTIME_PATH ${SERVER_ROOT_PATH}/runtime)

# TRPC framework path
set(TRPC_ROOT_PATH ${PLUGINS_PATH}/trpc)
message(STATUS "TRPC root path: ${TRPC_ROOT_PATH}")

# MongoDB driver path
set(MONGO_DRIVER_PATH ${PLUGINS_PATH}/mongo-cxx-driver)

# ============================================================================
# TRPC Framework Configuration
# ============================================================================
# Load TRPC config and utils
include(${TRPC_ROOT_PATH}/cmake/config/trpc_config.cmake)
include(${TRPC_ROOT_PATH}/cmake/tools/trpc_utils.cmake)

# Set protobuf tools
set(PB_PROTOC ${TRPC_ROOT_PATH}/bin/protoc)
set(TRPC_CPP_PLUGIN ${TRPC_ROOT_PATH}/bin/trpc_cpp_plugin)

# Set project dependency libraries
set(LIBRARY trpc ${LIBS_BASIC})
# Optional: Uncomment to use Prometheus metrics
# set(LIBRARY trpc ${LIBS_BASIC} ${LIB_METRICS_PROMETHEUS})

# ============================================================================
# Include and Link Directories
# ============================================================================
# Add TRPC include paths
include_directories(${INCLUDE_PATHS})
include_directories(${TRPC_ROOT_PATH}/include)

# Add MongoDB driver include paths
include_directories(
    ${MONGO_DRIVER_PATH}/include
    ${MONGO_DRIVER_PATH}/include/mongocxx/v_noabi
    ${MONGO_DRIVER_PATH}/include/bsoncxx/v_noabi
)

# Add library directories
link_directories(${LIBRARY_PATHS})
link_directories(${TRPC_ROOT_PATH}/lib)
link_directories(${TRPC_ROOT_PATH}/lib64)
link_directories(${MONGO_DRIVER_PATH}/lib64)
# ============================================================================
# Subdirectories
# ============================================================================
add_subdirectory(service_common)
add_subdirectory(helloworld)
add_subdirectory(helloworld_client)
add_subdirectory(puke)
# ============================================================================
# Installation Rules
# ============================================================================
# Install configuration files to runtime/bin subdirectories
install(DIRECTORY ${RUNTIME_PATH}/cfg/ 
        DESTINATION ${RUNTIME_PATH}/bin
        FILES_MATCHING
        PATTERN "*")

# Install shared libraries from plugins to each subdirectory's lib folder
install(CODE "
    # Find all shared libraries in plugins directory
    file(GLOB_RECURSE PLUGIN_LIBS \"${PLUGINS_PATH}/*/*.so*\")
    
    # Get all subdirectories in runtime/bin
    file(GLOB RUNTIME_SUBDIRS LIST_DIRECTORIES true \"${RUNTIME_PATH}/bin/*\")
    
    # Copy libraries to each subdirectory's lib folder
    foreach(SUBDIR \${RUNTIME_SUBDIRS})
        if(IS_DIRECTORY \${SUBDIR})
            # Create lib directory if not exists
            file(MAKE_DIRECTORY \"\${SUBDIR}/lib\")
            
            # Copy each library
            foreach(LIB \${PLUGIN_LIBS})
                get_filename_component(LIB_NAME \${LIB} NAME)
                # message(STATUS \"Installing \${LIB_NAME} to \${SUBDIR}/lib/\")
                file(COPY \${LIB} DESTINATION \"\${SUBDIR}/lib/\")
            endforeach()
        endif()
    endforeach()
    
    message(STATUS \"Shared libraries installation completed\")
")

# ============================================================================
# Summary
# ============================================================================
message(STATUS "")
message(STATUS "========================================")
message(STATUS "Project: ${PROJECT_NAME} v${PROJECT_VERSION}")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "C++ standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "TRPC path: ${TRPC_ROOT_PATH}")
message(STATUS "Runtime path: ${RUNTIME_PATH}")
message(STATUS "========================================")
message(STATUS "")